package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoTimeInfo;
import com.ocom.accesservice.service.TimeInfoService;
import com.ocom.common.entity.access.EntityMjTimeInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.DelAreaVo;
import com.ocom.common.vo.GroupListVo;
import com.ocom.common.vo.access.TimeInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class TimeInfoServiceImpl extends ServiceImpl<IDaoTimeInfo, EntityMjTimeInfo> implements TimeInfoService {


    @Autowired
    private IDaoTimeInfo daoTimeInfo;



    /**
     * 移除区域
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Transactional
    @Override
    public void RemoveArea(DelAreaVo delAreaVo,Long companyID) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(delAreaVo.getComId())){
            query.eq("com_id",delAreaVo.getComId());
        }
        if (StringUtils.isNotNull(delAreaVo.getAreaId())){
            query.eq("area_id",delAreaVo.getAreaId());
        }
        daoTimeInfo.delete(query);

    }

    /**
     * 初始化单位门禁参数
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    @Transactional
    @Override
    public void InitCompanyPara(EntityMjTimeInfo entityMjTimeInfo,Long companyId) {
        daoTimeInfo.insert(entityMjTimeInfo);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public List<EntityMjTimeInfo> getTimeInfoList(Long companyID, Long areaNo) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();

        query.eq("com_id",companyID);

        query.eq("area_id",areaNo);
        query.orderByAsc("group_no","zone_no","time_no");
        return daoTimeInfo.selectList(query);
    }

    //获取开门时段
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public List<EntityMjTimeInfo> getTimeList(Long companyID, TimeInfoVo timeInfoVo) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();

        query.eq("com_id",companyID);
        query.eq("area_id",timeInfoVo.getAreaNo());
        query.eq("group_no",timeInfoVo.getGroupNo());
        query.eq("zone_no",Long.valueOf(timeInfoVo.getZoneNo()));
        query.orderByAsc("begin_time");
        return daoTimeInfo.selectList(query);
    }

    //删除
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public void deleAccessTimeSlots(Long companyID, TimeInfoVo timeInfoVo) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();
        query.eq("com_id",timeInfoVo.getCompanyID());
        query.eq("area_id",timeInfoVo.getAreaNo());
        query.eq("group_no",timeInfoVo.getGroupNo());
        query.eq("zone_no",Long.valueOf(timeInfoVo.getZoneNo()));
        daoTimeInfo.delete(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public Boolean savesEntityMjTimeInfo(Long companyID, List<EntityMjTimeInfo> timeInfoList) {
        return this.saveBatch(timeInfoList);
    }
}
